﻿#region Copyright information
// 
// Copyright © 2012-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the SubnetworkToolkit and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: Gene.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@outlook.com
// - Date Created: 2013-01-30 2:55 PM
// - Last Modified: 2013-01-30 3:21 PM
// 
#endregion
using System;
using System.Collections.Generic;
using System.Globalization;

namespace SubnetworkToolkit
{
    public class Gene : IEquatable<Gene>, IComparable<Gene>, IFormattable
    {
        #region Public properties

        public int Id { get; set; }

        public string Symbol { get; set; }

        public double Statistic { get; private set; }

        public Dictionary<string, double> Property { get; private set; }

        #endregion

        #region Constructor

        public Gene(int id, double statistic = 0.0)
        {
            Id = id;
            Symbol = String.Empty;
            Statistic = statistic;
            Property = new Dictionary<string, double>();
        }

        public Gene(string symbol, double statistic = 0.0)
        {
            Id = -1;
            Symbol = symbol;
            Statistic = statistic;
            Property = new Dictionary<string, double>();
        }

        #endregion

        #region IEquatable<Gene> implementation

        public bool Equals(Gene other)
        {
            return Id.Equals(other.Id);
        }

        #endregion

        #region IFormattable implementation

        public string ToString(string format, IFormatProvider formatProvider)
        {
            if (format == null)
                format = "i";

            var lower = format.ToLower();

            return lower.StartsWith("s") ? Symbol : Id.ToString(CultureInfo.InvariantCulture);
        }

        #endregion

        #region Object overriden

        public override int GetHashCode()
        {
            return Id;
        }

        public override bool Equals(object other)
        {
            if (other == null)
                throw new ArgumentNullException();

            if (!(other is Gene))
                throw new InvalidCastException("The 'other' argument is not an Gene object.");

            return Equals(other as Gene);
        }

        public int CompareTo(Gene other)
        {
            return Id.CompareTo(other.Id);
        }

        public override string ToString()
        {
            return ToString("i", null);
        }

        #endregion
    }
}